package org.skydiveapp;

import org.skydiveapplibrary.repository.SettingsRepository;
import org.skydiveapplibrary.ui.InterfaceManager;
import org.skydiveapplibrary.R;
import org.skydiveapplibrary.ui.ExportDestinationAdapter;
import org.skydiveapplibrary.ui.ImportSourceAdapter;
import org.skydiveapplibrary.util.DropBoxClient;
import org.skydiveapplibrary.util.ExportDestination;
import org.skydiveapplibrary.util.ExportTask;
import org.skydiveapplibrary.util.ImportSource;
import org.skydiveapplibrary.util.ImportTask;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;

public class ImportExportActivity extends SherlockActivity
{
	// data
	private boolean isDualDisplay;
	
	// Button Listeners
	private OnClickListener exportListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			exportClicked();
		}
	};
	private OnClickListener importListener = new OnClickListener()
	{
		@Override
		public void onClick(View view)
		{
			importClicked();
		}
	};
	private OnClickListener logoutListener = new OnClickListener()
	{
		@Override
		public void onClick(View v)
		{
			_dropboxClient.logout();
			updateLogoutVisibility();
		}
	};

	// custom import task
	private class HDImportTask extends ImportTask
	{
		public HDImportTask()
		{
			super(ImportExportActivity.this);
		}
		
		@Override
		protected void onPostExecute(Integer result)
		{
			super.onPostExecute(result);
		}
	}
	
	// dropbox client
	private DropBoxClient _dropboxClient;
	// flags indicating importing/exporting for use after resume from dropbox authentication
	private boolean isExporting = false;
	private boolean isImporting = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		SettingsRepository settingsRepository = new SettingsRepository(this);
		if (settingsRepository.getUseLightTheme())
			super.setTheme(R.style.SkydiveAppTheme_Light_SemiTransparent);
		
		super.onCreate(savedInstanceState);
		
		// create dropbox client
		_dropboxClient = new DropBoxClient(this);
		
		// set flags
		isExporting = false;
		isImporting = false;
		
		setContentView(R.layout.import_export_view);
	
		isDualDisplay = this.findViewById(R.id.phoneLayout) == null;
		
		initializeUI();
	}
	
	private void initializeUI()
	{
		// set subtitle text		
    	InterfaceManager.getInstance().setSubtitleText(getSupportActionBar(), 
				getResources().getString(R.string.import_export_label));
		
		Spinner exportSpinner = (Spinner) findViewById(R.id.ImportExportExportSpinner);
		exportSpinner.setAdapter(new ExportDestinationAdapter(this));
		
		Button exportButton = (Button) findViewById(R.id.ImportExportExportButton);
		exportButton.setOnClickListener(exportListener);

		Spinner importSpinner = (Spinner) findViewById(R.id.ImportExportImportSpinner);
		importSpinner.setAdapter(new ImportSourceAdapter(this));
		
		Button importButton = (Button) findViewById(R.id.ImportExportImportButton);
		importButton.setOnClickListener(importListener);
		
		Button logoutButton = (Button) findViewById(R.id.DropBoxLogoutButton);
		logoutButton.setOnClickListener(logoutListener);
		
		// set logout visibility
		updateLogoutVisibility();
	}
	
	
    @Override
    public void onResume()
    {
        super.onResume();

        // finish dropbox authentication
        _dropboxClient.completeAuthentication();
        
        updateLogoutVisibility();
        
        if (_dropboxClient.isAuthenticated())
        {
	        if (isExporting)
	        	doExport();
	        else if (isImporting)
	        	doImport();
        }
        else
        {
        	isExporting = false;
    		isImporting = false;
        }
    }
	
	private void updateLogoutVisibility()
	{
		int visibility = _dropboxClient.isAuthenticated() ? View.VISIBLE : View.GONE;
		Button logoutButton = (Button) findViewById(R.id.DropBoxLogoutButton);
		logoutButton.setVisibility(visibility);
	}
	
	private void exportClicked()
	{	
		isExporting = true;
		isImporting = false;
		
		// get destination
		Spinner exportSpinner = (Spinner) findViewById(R.id.ImportExportExportSpinner);
		ExportDestination destination = (ExportDestination)exportSpinner.getSelectedItem();
		
		// check if authentication needed
		if (destination == ExportDestination.DropBox &&
			!_dropboxClient.isAuthenticated())
		{
			// authenticate
			_dropboxClient.startAuthentication();
		}
		else
		{
			// no authentication needed, just do export
			doExport();
		}
	}

	private void importClicked()
	{
		isExporting = false;
		isImporting = true;
		
		// get source
		Spinner importSpinner = (Spinner) findViewById(R.id.ImportExportImportSpinner);
		ImportSource source = (ImportSource)importSpinner.getSelectedItem();
		
		if (source == ImportSource.DropBox &&
			!_dropboxClient.isAuthenticated())
		{
			// authenticate
			_dropboxClient.startAuthentication();
		}
		else
		{
			doImport();
		}
	}
	
	private void doExport()
	{
		// get destination
		Spinner exportSpinner = (Spinner) findViewById(R.id.ImportExportExportSpinner);
		ExportDestination destination = (ExportDestination)exportSpinner.getSelectedItem();
		
		// create export task
		ExportTask task = new ExportTask(this);
		task.execute(destination);
		
		// clear flags
		isExporting = false;
		isImporting = false;
	}
	
	private void doImport()
	{
		// get source
		Spinner importSpinner = (Spinner) findViewById(R.id.ImportExportImportSpinner);
		ImportSource source = (ImportSource)importSpinner.getSelectedItem();

		// create import task
		ImportTask task = new HDImportTask();
		task.execute(source);
		
		// clear flags
		isExporting = false;
		isImporting = false;
	}

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.menu_back, menu);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
    	menu.findItem(R.id.menu_back).setVisible(isDualDisplay);
    	return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        int itemId = item.getItemId();
        
		if (itemId == R.id.menu_back)
		{
			// finish
			close();
			return true;
		}
		else
		{
			return super.onOptionsItemSelected(item);
		}
    }

	@Override
	public void onBackPressed()
	{
		close();
	}

	private void close()
	{
		// set result
		Intent intent = new Intent();
		setResult(RESULT_OK, intent);
		finish();
	}
}
